package com.guolibbs.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guolibbs.vo.User;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String opt = request.getParameter("opt");
		if("login".equals(opt)){
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			List<String> users = new ArrayList<String>();
			List<String> pswds = new ArrayList<String>();
			users.add("zxd");
			users.add("ljf");
			users.add("jlb");
			pswds.add("zxd");
			pswds.add("jlb");
			pswds.add("ljf");
			if (username.equals("") || !users.contains(username)) {
				request.setAttribute("err", "用户名不存在...");
				request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
			} else if (password.equals("") || !pswds.contains(password)) {
				request.setAttribute("err", "密码错误...");
				request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
			} else {
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				request.getSession().setAttribute("user", user);
				response.sendRedirect(this.getServletContext().getContextPath()+"/TopicOpt?opt=allUi");
			}
		}
		if("loginUi".equals(opt)){
			request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
		}
	}
}
